package com.example.demo.controller;

import com.example.demo.entity.Car;
import com.example.demo.entity.Goods;
import com.example.demo.entity.Result;
import com.example.demo.service.CarService;
import com.example.demo.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class CarController {

    @Autowired
    CarService carService;
    @Autowired
    GoodsService goodsService;

    @CrossOrigin
    @PostMapping(value = "/api/goodsToCar")
    @ResponseBody
    public Result goodsToCar(@RequestBody Car car)
    {
        Car search_car = carService.findbyid(car.getGoodsid(),car.getUserid());
        Goods goods = goodsService.findGoodsById(car.getGoodsid());
        if(search_car != null)
        {
            if(goods.getNum() <  search_car.getNum()+car.getNum())
            {
                carService.updateNum(search_car.getGoodsid(), goods.getNum(), search_car.getUserid());
            }
            else
            {
                carService.updateNum(search_car.getGoodsid(), search_car.getNum()+car.getNum(), search_car.getUserid());
            }

            Result result = new Result(200);
            return result;
        }
        carService.insertCar(car.getGoodsid(),car.getNum(),car.getUserid());

        Result result = new Result(200);
        return result;
    }

    @CrossOrigin
    @PostMapping(value = "/api/updateNum")
    @ResponseBody
    public Result addNum(@RequestBody Car car)
    {

        carService.updateNum(car.getGoodsid(), car.getNum(), car.getUserid());
        Result result = new Result(200);
        return result;
    }

    @CrossOrigin
    @PostMapping(value = "/api/clearCar")
    @ResponseBody
    public Result clearCar(@RequestBody Car car)
    {
        carService.clearCar(car.getUserid());
        Result result = new Result(200);
        return result;
    }

    @CrossOrigin
    @PostMapping(value = "/api/removeFromCar")
    @ResponseBody
    public Result removeFromCar(@RequestBody Car car)
    {
        System.out.println(car.getGoodsid()+"   "+car.getUserid());
        carService.removeFromCar(car.getGoodsid(),car.getUserid());
        Result result = new Result(200);
        return result;
    }

    @CrossOrigin
    @PostMapping(value = "/api/findUserCar")
    @ResponseBody
    public Result findUserCar(@RequestBody Car car)
    {

        List<Car> cars = carService.findUserCar(car.getUserid());
        Goods goods;

        for(int i = 0 ; i < cars.size() ; i++)
        {
            goods = goodsService.findGoodsById(cars.get(i).getGoodsid());
            cars.get(i).setGoods(goods);
        }
        Result result = new Result(200);
        result.setData(cars);
        return result;
    }

}
